﻿using System;
using Autodesk.Revit.DB;

namespace YJKInspectModel.ModelValidation.Utils
{
	public class EdgeInfo
	{
		public XYZ StartPoint
		{
			get
			{
				return this.m_StartPoint;
			}
			set
			{
				this.m_StartPoint = value;
			}
		}

		public XYZ EndPoint
		{
			get
			{
				return this.m_EndPoint;
			}
			set
			{
				this.m_EndPoint = value;
			}
		}

		public double Bulge
		{
			get
			{
				return this.m_Bulge;
			}
			set
			{
				this.m_Bulge = value;
			}
		}

		public bool IsArc
		{
			get
			{
				return !Geometry.IsEqual(this.m_Bulge, 0.0);
			}
		}

		public XYZ Center
		{
			get
			{
				if (Geometry.IsEqual(this.m_Bulge, 0.0))
				{
					return null;
				}
				XYZ result = null;
				Geometry.GetCenterWithBulge(this.m_StartPoint, this.m_EndPoint, this.m_Bulge, ref result);
				return result;
			}
		}

		public bool IsNull()
		{
			return this.m_StartPoint == null || this.m_EndPoint == null;
		}

		public EdgeInfo(EdgeInfo rhs)
		{
			this.m_StartPoint = rhs.m_StartPoint;
			this.m_EndPoint = rhs.m_EndPoint;
			this.m_Bulge = rhs.m_Bulge;
		}

		public EdgeInfo(XYZ startPoint, XYZ endPoint, double bulge)
		{
			this.m_StartPoint = startPoint;
			this.m_EndPoint = endPoint;
			this.m_Bulge = bulge;
		}

		public EdgeInfo(Curve curve)
		{
			if (curve.GetType() == typeof(Line))
			{
				this.m_StartPoint = curve.GetEndPoint(0);
				this.m_EndPoint = curve.GetEndPoint(1);
				this.m_Bulge = 0.0;
				return;
			}
			if (curve.GetType() == typeof(Arc))
			{
				Arc arc = curve as Arc;
				this.m_StartPoint = curve.GetEndPoint(0);
				this.m_EndPoint = curve.GetEndPoint(1);
				if (!Geometry.IsEqual(arc.Normal, XYZ.BasisZ))
				{
					this.m_StartPoint = curve.GetEndPoint(1);
					this.m_EndPoint = curve.GetEndPoint(0);
				}
				this.m_Bulge = Geometry.GetBulge(this.m_StartPoint, this.m_EndPoint, arc.Center, true);
			}
		}

		public EdgeInfo TransformTo(Transform matrix)
		{
			XYZ startPoint = Geometry.TransformPoint(this.StartPoint, matrix);
			XYZ endPoint = Geometry.TransformPoint(this.EndPoint, matrix);
			return new EdgeInfo(startPoint, endPoint, this.m_Bulge);
		}

		public void TransformSelf(Transform matrix)
		{
			this.m_StartPoint = Geometry.TransformPoint(this.StartPoint, matrix);
			this.m_EndPoint = Geometry.TransformPoint(this.EndPoint, matrix);
		}

		public double Length()
		{
			if (this.IsArc)
			{
				XYZ xyz = null;
				Geometry.GetCenterWithBulge(this.StartPoint, this.EndPoint, this.Bulge, ref xyz);
				double num = xyz.DistanceTo(this.StartPoint);
				double angle = Geometry.getAngle(xyz, this.StartPoint);
				double angle2 = Geometry.getAngle(xyz, this.EndPoint);
				double num2 = Geometry.BetweenTheAngles(angle, angle2, true);
				if (Geometry.LessThan(this.Bulge, 0.0))
				{
					num2 = Geometry.BetweenTheAngles(angle2, angle, true);
				}
				return num2 * num;
			}
			return this.StartPoint.DistanceTo(this.EndPoint);
		}

		private XYZ Get2DXYZ(XYZ point3d)
		{
			return new XYZ(point3d.X, point3d.Y, 0.0);
		}

		private XYZ m_StartPoint;

		private XYZ m_EndPoint;

		private double m_Bulge;
	}
}
